package com.xidian.zhiancloud.service.accidentManage;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.sun.xml.bind.v2.runtime.reflect.Lister;
import com.xidian.zhiancloud.dao.AccidentCaseMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.AccidentCase;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.accidentmessage.AccidentCaseQO;
import com.xidian.zhiancloud.vo.accidentmessage.AccidentCaseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName AccidentCaseService
 * @Description TODO
 * @Author Halo
 **/
@Service
public class AccidentCaseService extends ServiceImpl<AccidentCaseMapper, AccidentCase> implements IService<AccidentCase> {

    @Autowired
    private AccidentCaseMapper accidentCaseMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 根据id删除事故案例
     * @param ids 事故案例id数组
     * @return 返回是否删除成功
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    public Boolean deleteByIds(Long[] ids){
        for (Long id : ids) {
            int res = accidentCaseMapper.deleteById(id);
            if(res == 0){
                throw new CustomizeException(GeneralResponseEnums.DELETE_FAILED);
            }
        }
        return true;
    }

    /**
     * 根据id查询事故案例信息
     * @param id
     * @return
     */
    public AccidentCaseVO getMsgById(Long id){
        AccidentCase accidentCase = accidentCaseMapper.selectById(id);

        AccidentCaseVO accidentCaseVO = new AccidentCaseVO();
        BeanUtils.copyProperties(accidentCase,accidentCaseVO);

        SysUser creatorUser = sysUserMapper.selectById(accidentCase.getCreator());

        accidentCaseVO.setCreatorName(creatorUser == null ? "该用户不存在" : creatorUser.getName());

        return accidentCaseVO;
    }

    /**
     * 列表查询
     * @param accidentCaseQO
     * @return
     */
    public IPage<AccidentCaseVO> getListByQO(AccidentCaseQO accidentCaseQO){
        Page<AccidentCaseVO> page = new Page<>(accidentCaseQO.getCurrent(), accidentCaseQO.getSize());

        //排序
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);

        IPage<AccidentCaseVO> list = accidentCaseMapper.getListByQO(page,accidentCaseQO);

        list.getRecords().forEach(vo -> {
            vo.setCreatorName(vo.getCreatorName() == null ? "该用户不存在" : vo.getCreatorName());
        });

        return list;
    }
}
